import React from 'react';
import CodeEditor from './components/CodeEditor';
import FileView from './components/FileView';
import UserState from './core/UserState';
import Addon from './core/Addon';

function renderScene(route, navigator) {
  return <route.component route={route} navigator={navigator} file={route.file} />;
}

Addon.load();

export default class extends React.Component {
  constructor() {
    super();
    this.state = {
      last: null
    };
  }

  componentWillMount() {
    let me = this;
    UserState.getState().then(state => {
      Addon.activate(state);
      me.setState(state);
    });
  }

  render() {
    const initialRoute = {
      component: (this.state.last && this.state.last.isFile) ? CodeEditor : FileView,
      file: this.state.last
    };

    return (
      <View style={{ flex: 1, }}>
        <Navigator
          initialRoute={initialRoute}
          renderScene={renderScene}/>
      </View>
    );
  }
}
